using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;
using System.IO;

public partial class miniweb_Default : System.Web.UI.Page
{
    protected long Id
    {
        get
        {
            object o = ViewState["Id"];
            if (o != null)
                return (long)o;
            return long.MinValue;
        }
        set
        {
            ViewState["Id"] = value;
        }
    }
    ProductSellCollection _ProductSellCollection = new ProductSellCollection();
    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlAnchor aMenuHome = (HtmlAnchor)Page.Master.FindControl("aMenuHome");
        aMenuHome.Attributes.Add("class", "Select");
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            this.Id = long.Parse(Request.QueryString["id"]);
        if (IsPostBack)
            return;
        LoadData();
        LoadProductShowCase();
        LoadProductNew();

    }
    #region Load Data Company
    private void LoadData()
    {


        if (this.Id != long.MinValue && this.Id != 0)
        {
            Company _Company = CompanyService.GetCompany(this.Id);
            if (_Company != null)
            {
                Literal ltrPageTitle = (Literal)Master.FindControl("ltrPageTitle");
                ltrPageTitle.Text = _Company.CompanyName;
                Session["MemberID"] = _Company.MemberID;
                Session["CompanyID"] = _Company.CompanyID;
                ltrDes.Text = Utilitys.ShortSummary(400, _Company.CompanyInfoHome);
                if (!string.IsNullOrEmpty(_Company.Logo))
                {
                    imgCompany.Src = "../photos/" + _Company.Logo;
                }
                else
                    div.Attributes.Add("style", "background:none;");
            }

        }
        else if (Session["CompanyID"] != null)
        {
            this.Id = long.Parse(Session["CompanyID"].ToString());
            Company _Company = CompanyService.GetCompany(this.Id);
            if (_Company != null)
            {
                Literal ltrPageTitle = (Literal)Master.FindControl("ltrPageTitle");
                ltrPageTitle.Text = _Company.CompanyName;
                Session["MemberID"] = _Company.MemberID;
                Session["CompanyID"] = _Company.CompanyID;
                ltrDes.Text = Utilitys.ShortSummary(400, _Company.CompanyInfoHome);
                if (!string.IsNullOrEmpty(_Company.Logo))
                {
                    imgCompany.Src = "../photos/" + _Company.Logo;
                }
                else
                    div.Visible = false;
            }
        }
    }
    #endregion
    #region Load Product of company
    private void LoadProductShowCase()
    {
        if (Session["MemberID"] != null)
        {
            this.Id = long.Parse(Session["MemberID"].ToString());

            int total = 0;
            Criteria cri = new Criteria();
            cri.add(ProductSell._Table + "." + ProductSellColumns.MemberId, this.Id, Condition.EQUAL, Condition.AND);
            cri.add(ProductSell._Table + "." + ProductSellColumns.Enable, 1, Condition.EQUAL, Condition.AND);
            _ProductSellCollection = ProductSellService.GetProductSellListByParam("[" + ProductSell._Table + "].[" + ProductSellColumns.Order + "]," + "[" + ProductSell._Table + "].[" + ProductSellColumns.CreatedDate + "]", OrderDirection.DESC, cri.Criter);
            if (_ProductSellCollection.Count > 0)
            {
                rptProductShowCase.DataSource = _ProductSellCollection;
                rptProductShowCase.DataBind();
            }


        }
    }
    protected void rptProductShowCase_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            ProductSell _ProductSell = (ProductSell)e.Item.DataItem;
            Literal ltrTitleProduct = (Literal)e.Item.FindControl("ltrTitleProduct");
            HtmlAnchor ahrImg = (HtmlAnchor)e.Item.FindControl("ahrImg");
            HtmlGenericControl li = (HtmlGenericControl)e.Item.FindControl("li");
            HtmlAnchor ahrTitle = (HtmlAnchor)e.Item.FindControl("ahrTitle");
            HtmlImage imgProduct = (HtmlImage)e.Item.FindControl("imgProduct");
            ltrTitleProduct.Text = Utilitys.ShortSummary(20, _ProductSell.Title);
            ahrTitle.HRef = string.Format("ProductDetail.aspx?id={0}", _ProductSell.ProductId);
            ahrImg.HRef = string.Format("ProductDetail.aspx?id={0}", _ProductSell.ProductId);
            if (!string.IsNullOrEmpty(_ProductSell.Image))
            {
                imgProduct.Src = "../photos/" + _ProductSell.Image;
            }
            if (e.Item.ItemIndex == _ProductSellCollection.Count)
                li.Attributes.Add("class", "end");
        }


    }

    #endregion

    #region Load Product New
    private void LoadProductNew()
    {
        if (Session["MemberID"] != null)
        {
            this.Id = long.Parse(Session["MemberID"].ToString());

            Criteria cri = new Criteria();
            int total = 0;
            cri.add(ProductSell._Table + "." + ProductSellColumns.MemberId, this.Id, Condition.EQUAL, Condition.AND);
            cri.add(ProductSell._Table + "." + ProductSellColumns.Enable, 1, Condition.EQUAL, Condition.AND);
            _ProductSellCollection = ProductSellService.GetProductSellListByParam("[" + ProductSell._Table + "].[" + ProductSellColumns.Order + "]," + "[" + ProductSell._Table + "].[" + ProductSellColumns.CreatedDate + "]", OrderDirection.DESC, 1, 8, cri.Criter, out total);
            if (_ProductSellCollection.Count > 0)
            {
                rptProductNew.DataSource = _ProductSellCollection;
                rptProductNew.DataBind();
            }




        }
    }
    protected void rptProductNew_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            ProductSell _ProductSell = (ProductSell)e.Item.DataItem;
            Literal ltrTitleProduct = (Literal)e.Item.FindControl("ltrTitleproductNew");
            HtmlAnchor ahrImg = (HtmlAnchor)e.Item.FindControl("ahrImg");
            HtmlAnchor ahrTitle = (HtmlAnchor)e.Item.FindControl("ahrTitle");
            HtmlImage imgProduct = (HtmlImage)e.Item.FindControl("imgProductNew");
            ltrTitleProduct.Text = Utilitys.ShortSummary(23, _ProductSell.Title);
            ahrTitle.HRef = string.Format("ProductDetail.aspx?id={0}", _ProductSell.ProductId);
            ahrImg.HRef = string.Format("ProductDetail.aspx?id={0}", _ProductSell.ProductId);
            if (!string.IsNullOrEmpty(_ProductSell.Image))
            {
                imgProduct.Src = "../photos/" + _ProductSell.Image;
            }

        }


    }

    #endregion
}
